package com.skivingcloud.common.utils;

/**
 * @author hushouquan
 * @since 2024/5/9
 */
public class SkivingStringUtil{
    private SkivingStringUtil(){
        super();
    }
    /**
     * sql中like条件参数两边加%
     * @param param 参数
     * @return 拼接后字符串
     */
    public static String getLikeSqlFormat(String param){
        return "%" + param + "%";
    }
    /**
     * sql中like条件参数前面加%
     * @param param 参数
     * @return 拼接后字符串
     */
    public static String getLeftLikeSqlFormat(String param){
        return "%" + param;
    }
    /**
     * sql中like条件参数后面加%
     * @param param 参数
     * @return 拼接后字符串
     */
    public static String getRightLikeSqlFormat(String param){
        return param + "%";
    }

    /**
     * 首字母转小写
     * @param str 要转换的字符串
     * @return 转换后字符串
     */
    public static String lowerFirst(CharSequence str) {
        if (null == str) {
            return null;
        } else {
            if (!str.isEmpty()) {
                char firstChar = str.charAt(0);
                if (Character.isUpperCase(firstChar)) {
                    return Character.toLowerCase(firstChar) + subSuf(str, 1);
                }
            }

            return str.toString();
        }
    }

    public static String subSuf(CharSequence string, int fromIndex) {
        return isEmpty(string) ? null : sub(string, fromIndex, string.length());
    }

    public static boolean isEmpty(CharSequence str) {
        return str == null || str.isEmpty();
    }

    public static String sub(CharSequence str, int fromIndexInclude, int toIndexExclude) {
        if (isEmpty(str)) {
            return str(str);
        } else {
            int len = str.length();
            if (fromIndexInclude < 0) {
                fromIndexInclude += len;
                if (fromIndexInclude < 0) {
                    fromIndexInclude = 0;
                }
            } else if (fromIndexInclude > len) {
                fromIndexInclude = len;
            }

            if (toIndexExclude < 0) {
                toIndexExclude += len;
                if (toIndexExclude < 0) {
                    toIndexExclude = len;
                }
            } else if (toIndexExclude > len) {
                toIndexExclude = len;
            }

            if (toIndexExclude < fromIndexInclude) {
                int tmp = fromIndexInclude;
                fromIndexInclude = toIndexExclude;
                toIndexExclude = tmp;
            }

            return fromIndexInclude == toIndexExclude ? "" : str.toString().substring(fromIndexInclude, toIndexExclude);
        }
    }

    public static String str(CharSequence cs) {
        return null == cs ? null : cs.toString();
    }
}
